﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Cells;
using Esint.Common;
using Esint.JudicialAuction.Model;
using Esint.JudicialAuction.SqlDAL;

namespace WebJudicialAuction
{
    public partial class TempPowerHelper : System.Web.UI.Page
    {
        Sys_CodeData codeDal = new Sys_CodeData();
        Sys_MenuData menuDal = new Sys_MenuData();

        protected void Page_Load(object sender, EventArgs e)
        {
            var filePath = @"D:\Personal\exercises\fapai\权限点总结.xlsx";

            Workbook workbook = new Workbook(filePath);

            Worksheet worksheet = workbook.Worksheets["Sheet1"];

            var rowIndex = 1;
            var lastLv1Id = string.Empty;
            var lastLv2Id = string.Empty;

            StringBuilder sb = new StringBuilder();

            while (worksheet.Cells[rowIndex, 0].StringValue.IsNotNullOrEmpty() || worksheet.Cells[rowIndex, 1].StringValue.IsNotNullOrEmpty() || worksheet.Cells[rowIndex, 2].StringValue.IsNotNullOrEmpty())
            {
                try
                {
                    if (worksheet.Cells[rowIndex, 4].StringValue.IsNotNullOrEmpty())
                    {
                        rowIndex++;
                        continue;
                    }

                    string typeMeaning = worksheet.Cells[rowIndex, 3].StringValue;
                    string menuName = string.Empty;
                    string parentId = null;
                    string menuId = Guid.NewGuid().ToString();
                    string rootId = null;
                    int menuLevel = 0;

                    if (worksheet.Cells[rowIndex, 0].StringValue.IsNotNullOrEmpty())
                    {
                        menuName = worksheet.Cells[rowIndex, 0].StringValue;
                        lastLv1Id = menuId;
                        menuLevel = 1;
                        parentId = null;
                        rootId = null;
                    }
                    else if (worksheet.Cells[rowIndex, 1].StringValue.IsNotNullOrEmpty())
                    {
                        menuName = worksheet.Cells[rowIndex, 1].StringValue;
                        lastLv2Id = menuId;
                        parentId = lastLv1Id;
                        menuLevel = 2;
                        rootId = lastLv1Id;
                    }
                    else if (worksheet.Cells[rowIndex, 2].StringValue.IsNotNullOrEmpty())
                    {
                        menuName = worksheet.Cells[rowIndex, 2].StringValue;
                        parentId = lastLv2Id;
                        menuLevel = 3;
                        rootId = lastLv1Id;
                    }

                    Sys_MenuInfo sys_MenuInfo = new Sys_MenuInfo() { MenuId = menuId, MenuName = menuName, Status = "0", MenuType = TransferCodeFormStatusMeaning(typeMeaning), ParentId = parentId, Level = menuLevel, RootId=rootId };
                    menuDal.Insert(sys_MenuInfo);

                    worksheet.Cells[rowIndex, 4].PutValue(menuId);
                }
                catch (Exception ex)
                {
                    sb.AppendLine($"第{ (rowIndex + 1)}行导入失败");
                    CommonExtend.ErrorLog(ex, $"权限excel导入：第{(rowIndex + 1)}行导入失败");
                }

                rowIndex++;
            }

            if (sb.ToString().IsNullOrEmpty())
            {
                workbook.Save(filePath);
            }

            serverMsg.InnerText = sb.ToString().IsNullOrEmpty() ? "全部导入成功" : sb.ToString();
        }

        string TransferCodeFormStatusMeaning(string meaning)
        {
            var codeObj = codeDal.GetCodeObjectByFlagAndMeaning("6", meaning);
            return codeObj.Code;
        }
    }
}